﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBSourceToolsLib.SysObjects;
using DBConnectionLib;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    [Ignore]
    public class TestDatabaseList
    {
        private const string strControlPath = BaseDirectoryHelper.Unused + "\\control";
        private const string strOutputPath = BaseDirectoryHelper.Unused + "\\output";

        [Test]
        
        public void TestLoadList()
        {
            DatabaseList oDatabaseList = new DatabaseList();
            oDatabaseList.Load(strControlPath + "\\AdventureWorks\\DeploymentTargets", "", true);
            
            Assert.Greater(oDatabaseList.Count, 0, "Database List should contain more than 0 databases");
        }

        [Test]
        public void TestGetDatabase()
        {
            DatabaseList oDatabaseList = new DatabaseList();
            oDatabaseList.Load(strControlPath + "\\AdventureWorks\\DeploymentTargets", "",true);
            
            Assert.Greater(oDatabaseList.Count, 0, "Database List should contain more than 0 databases");
            Database oDatabase = oDatabaseList.getDatabase("localcopy");
            Assert.IsNotNull(oDatabase, "DatabaseList should return a database");
        }

        [Test]
        public void TestSaveDatabaseList()
        {
            DatabaseList oDatabaseList = new DatabaseList(strOutputPath + "\\TestDBList");
            oDatabaseList.AddDatabase(new Database("TestNick", ".", "TestDb", "testdir"));
            oDatabaseList.AddDatabase(new Database("TestNick2", ".", "TestDb2", "testdir2"));
            oDatabaseList.Save();

            DBSourceToolsLib.FileComparer.FileComparer oFileComparer
                = new DBSourceToolsLib.FileComparer.FileComparer(strControlPath + "\\TestDBList\\Databases.xml",
                    strOutputPath + "\\TestDBList\\Databases.xml");
            Assert.IsTrue(oFileComparer.AreFilesEqual(), "Invalid " + strOutputPath + "\\TestDBList\\Databases.xml");
            

        }

        [Test]
        public void TestConnectionString()
        {
            DatabaseList oDatabaseList = new DatabaseList(strControlPath + "\\TestDBListConnectionString");
            
            Database oDatabase = oDatabaseList.getDatabase("AdventureWorks");
            Assert.IsTrue(oDatabase.ConnectionString.CompareTo("Data Source=.;Initial Catalog=AdventureWorks;Integrated Security=True;") == 0, "Connection string incorrect");

            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(oDatabase.ConnectionString))
            {
                conn.Open();
                Assert.IsNotNull(conn, " could not connect to ");
                conn.Close();
            }

        }
    }
}
